<template>
	<view class="page">
		<view class="body">
			<navigator open-type="switchTab" hover-class="none" url="/pages/article/article" class="iconfont close-btn icon-guanbi" ></navigator> 
			<view class="banner">
				<image src="@/static/images/login_bg.png" mode="widthFix"></image>
			</view>

			<view class="login-wrap">
				<view class="login-form active">
					<!-- 手机登录 -->
					<template v-if="isMb">
						<view class="item">
							<view class="icon">
								<image src="@/static/images/phone_icon.png" mode="widthFix"></image>
							</view>
							<input type="number" adjust-position="false" v-model="phone" placeholder="请输入手机号" />
						</view>
						<view class="item">
							<view class="icon">
								<image src="@/static/images/phone_code.png" mode="widthFix"></image>
							</view>
							<input type="number" adjust-position="false" v-model="phone_code" placeholder="输入验证码" />
							<view v-if="isSentCode" class="get-code-btn" @click="sendSms">{{count_time_text}}</view>
							<view v-else class="get-code-btn">{{count_time_text}}</view>
						</view>
					</template>

					<!-- 账号密码登录 -->
					<template v-else>
						<view class="item">
							<view class="icon">
								<image src="@/static/images/user.png" mode="widthFix"></image>
							</view>
							<input type="text" adjust-position="false" v-model="user_name" value=""
								placeholder="请输入登录账号" />
						</view>
						<view class="item">
							<view class="icon">
								<image src="@/static/images/pw.png" mode="widthFix"></image>
							</view>
							<input type="password" adjust-position="false" v-model="password" placeholder="请输入登录密码" />
						</view>
						<view class="link-bar">
							<label class="re-pw" @click="remberPw">
								<radio color="#ff9600" :checked="isRemberPw"></radio>
								<view class="re-pw-text">记住密码</view>
							</label>
							<view class="find-pw-text" @click="findPwd">忘记密码</view>
						</view>
					</template>

					<view class="btn" @click="login">注册</view>
					<navigator url="/pages/login/login" open-type="redirect" hover-class="none" class="register-text">已有账号，去登录</navigator>
					<label class="agree-bar" @click="setAgree">
						<radio color="#ff9600" :checked="isAgree"></radio>
						<view class="re-pw-text">
							<text>同意并阅读</text>
							<navigator open-type="navigate" url="/package_member/authorization/authorization?type=2"
								hover-class="none">《隐私协议及用户服务协议》</navigator>
						</view>
					</label>
				</view>

			</view>

		</view>

		<view v-if="flag.isFullPage" class="full-fix-page">
			<!-- 验证手机 -->
			<view v-if="flag.isCheckPhone" class="popup-wrap a-fadein">
				<view class="popup-type-model ">
					<view class="close-btn iconfont f-s40" @click="closeCheckPhone">&#xe624;</view>
					<view class="popup-body">
						<view class="title">验证手机</view>
						<view class="content get-code-popup">
							<view class="code-bar">
								<input type="number" v-model="user_name" placeholder="输入手机号">
							</view>
							<view class="code-bar">
								<input type="number" v-model="phone_code" placeholder="输入验证码">
								<view v-if="isSentCode" class="get-code-btn" @click="sendSms">{{count_time_text}}</view>
								<view v-else class="get-code-btn">{{count_time_text}}</view>
							</view>
							<view class="code-comfirm-btn active" @click="checkSms">{{phone_code_type==0?'确定':'下一步'}}
							</view>


						</view>
					</view>
				</view>
			</view>

			<!-- 设置新密码 -->
			<view v-if="flag.isRestPassword" class="popup-wrap a-fadein">
				<view class="popup-type-model ">
					<view class="close-btn iconfont f-s40" @click="closeResetPassword">&#xe624;</view>
					<view class="popup-body">
						<view class="title">设置新密码</view>
						<view class="content get-code-popup">
							<view class="code-bar">
								<input type="password" v-model="new_pw" placeholder="输入新密码">
							</view>
							<view class="code-bar">
								<input type="password" v-model="new_pw1" placeholder="确认新密码">
							</view>
							<view class="code-comfirm-btn active" @click="resetPassword">确定</view>
						</view>
					</view>
				</view>
			</view>


		</view>
	</view>
</template>

<script>
	import {
		replaceStr,
		checkPhone
	} from '@/common/util.js'
	import Timer from '@/common/timer.js'
	import {
		HTTP_REQUEST_URL
	} from '@/config.js'
	export default {
		name: '',
		data() {
			return {
				isRemberPw: true,
				password: '' || uni.getStorageSync('password'),
				user_name: '' || uni.getStorageSync('user_name'),
				new_pw: '',
				new_pw1: '',
				phone: '',
				replace_phone: '',
				phone_code: '',
				phone_code_type: '3', // 0/空 密码登录   1 找回密码  2 重置密码  3 登录 
				count_time_text: '获取验证码',
				isSentCode: true,
				isStop: true,
				isMb: true,
				flag: {
					isFullPage: false,
					isCheckPhone: false,
					isRestPassword: false
				},
				isAgree: false,
				timer: null,
				userInfo: {},
				backType: ''

			}
		},
		async onLoad(params) {
			this.replace_phone = replaceStr(this.user_name, 3, '****')
			if (params.backType) {
				this.backType = params.backType;
			}
		},
		async onShow() {},
		methods: {

			// 记住密码
			remberPw: function() {
				this.isRemberPw = !this.isRemberPw;
			},

			// 同意协议
			setAgree: function() {
				this.isAgree = !this.isAgree;
			},

			// 登录
			login: async function() {
				let _this = this;
				if (!this.isAgree) {
					uni.showToast({
						icon: "none",
						title: "请先同意隐私和用户服务协议",
						mask: true
					})
					return
				}
				if (this.phone_code_type == 3) {
					if (this.phone == '') {
						uni.showToast({
							icon: "none",
							title: "请输入手机号",
							mask: true
						})
						return
					} else if (this.phone_code == '') {
						uni.showToast({
							icon: "none",
							title: "请输入手机验证码",
							mask: true
						})
						return
					} else if (!checkPhone(this.phone)) {
						uni.showToast({
							icon: "none",
							title: "请输入正确手机号码",
							mask: true
						})
						return
					}

					if (this.isStop) {
						this.isStop = false;
						let res = await this.$api.user_api.checksms({
							code: this.phone_code,
							type: this.phone_code_type
						}) 
						uni.showLoading({
							mask: true,
							title: '登录中...'
						})
						console.log(res)
						if (res.data.ret==0) {
							uni.hideLoading();
							this.phone_code = '';
							uni.showToast({
								icon: "success",
								title: "登录成功",
								mask: true,
								duration: 1000,
								async success() {
									let save_cookie = await _this.$api.user_api.saveCookie(); 
									_this.replace_phone = replaceStr(res.data.data.phone, 3, '****');
									_this.userInfo = res.data.data;
									
									uni.setStorage({
										key: 'userInfo',
										data: _this.userInfo
									})
									setTimeout(() => {
										uni.switchTab({
											url: "/pages/index/index"
										})
									}, 300)
									_this.isStop = true;
								}
							})

						} else {
							uni.hideLoading();
							uni.showToast({
								icon: "none",
								image: "../../static/images/error.png",
								title: res.data.message,
								mask: true
							})
							setTimeout(() => {
								_this.isStop = true;
							}, 300)
						}

					}


				}

				if (this.phone_code_type == 0) {
					if (this.user_name == '') {
						uni.showToast({
							icon: "none",
							title: "请输入登录账号",
							mask: true
						})
						return
					} else if (this.password == '') {
						uni.showToast({
							icon: "none",
							title: "请输入登录密码",
							mask: true
						})
						return
					}

					if (!this.isAgree) {
						uni.showToast({
							icon: "none",
							title: "请先同意隐私和用户服务协议",
							mask: true
						})
						return
					}

					if (this.isStop) {
						this.isStop = false;

						uni.showLoading({
							mask: true,
							title: '正在登陆...'
						})

						uni.request({
							url: HTTP_REQUEST_URL + '/user/login',
							method: 'POST',
							data: {
								username: this.user_name,
								password: this.password,
								is_app: 1
							},
							dataType: 'json',
							header: {
								"Content-Type": "application/x-www-form-urlencoded",
								"Cookie": []
							},
							success: (res) => {
								if (res.data.ret == 0) { 
									_this.phone = res.data.data.phone;
									res.data.data.alltype = res.data.data.alltype.split(','); // 权限字符串转数组
									_this.replace_phone = replaceStr(res.data.data.phone, 3, '****');
									_this.userInfo = res.data.data;

									setTimeout(() => {
										_this.isStop = true;
									}, 300)
									uni.hideLoading();

									if (_this.isRemberPw) {
										uni.setStorage({
											'key': 'user_name',
											'data': _this.user_name
										})
										uni.setStorage({
											'key': 'password',
											'data': _this.password
										})
									} else {
										uni.removeStorageSync('user_name');
										uni.removeStorageSync('password');
									}

									uni.setStorage({
										'key': 'cookiekey',
										'data': res.header['Set-Cookie']
									})
									_this.showPopup('show', 'isCheckPhone');


								} else {
									setTimeout(() => {
										_this.isStop = true;
									}, 300)
									uni.showToast({
										icon: "none",
										title: res.data.message,
										mask: true
									})
								}
							},
							fail: (err) => {

							}
						})



					}
				}



			},

			// 关闭验证手机
			closeCheckPhone: function() {
				this.showPopup('hide');
				this.count_time_text = '获取验证码';
				setTimeout(() => {
					this.isSentCode = true;
					this.phone_code = '';
				}, 300)
				if (this.timer) {
					this.timer.stop();
				}
			},

			// 关闭重置密码
			closeResetPassword: function() {
				this.showPopup('hide');
				setTimeout(() => {
					this.new_pw = '';
					this.new_pw1 = '';
				}, 300)
			},

			// 忘记密码
			findPwd: function() {
				this.phone_code_type = 1;
				this.showPopup('show', 'isCheckPhone')
			},

			// 重置密码
			resetPassword: async function() {
				let _this = this;
				if (this.new_pw == '') {
					uni.showToast({
						icon: 'none',
						title: '请输入新密码',
						mask: true,
						duration: 2000
					})
				} else if (this.new_pw1 == '') {
					uni.showToast({
						icon: 'none',
						title: '请再次输入新密码',
						mask: true,
						duration: 2000
					})
				} else if (this.new_pw != this.new_pw1) {
					uni.showToast({
						icon: 'none',
						title: '两次输入密码不一致',
						mask: true,
						duration: 2000
					})
				} else {
					if (this.isStop) {
						this.isStop = false;
						let isUpdateSuccess = await this.updatePwd();
						if (isUpdateSuccess) {
							this.showPopup('hide');
							this.password = '';
							uni.removeStorageSync('password');
							setTimeout(() => {
								_this.isStop = true;
							}, 1000)
						} else {
							setTimeout(() => {
								_this.isStop = true;
							}, 1000)
						}

					}
				}
			},

			updatePwd: async function() {
				let _this = this;

				let res = await this.$api.user_api.updatePwd({
					password: this.new_pw,
					phone: this.user_name
				})
				if (res.data.ret == 0) {
					uni.showToast({
						icon: 'success',
						title: '新密码设置成功',
						mask: true,
						duration: 1000,
						success() {

						}
					})
					return true;
				} else {
					uni.showToast({
						icon: 'none',
						image: '../../static/images/error.png',
						title: res.data.message,
						mask: true,
						duration: 1000,
						success() {

						}
					})
					return false;
				}
			},

			//发送验证码
			sendSms: async function() {
				// 1 找回密码  2 重置密码  3 手机登录  空/0 密码登录
				let _this = this;
				this.phone_code = "";
				if (this.phone_code_type == 3) {
					if (this.phone == '') {
						uni.showToast({
							icon: "none",
							title: "请输入手机号",
							mask: true
						})
						return
					}
					if (!checkPhone(this.phone)) {
						uni.showToast({
							icon: "none",
							title: "请输入正确手机号",
							mask: true
						})
						return
					}
					if (this.isSentCode) {
						this.isSentCode = false;
						uni.request({
							url: HTTP_REQUEST_URL + '/user/sendsms',
							method: 'POST',
							data: {
								tel: _this.phone,
								type: _this.phone_code_type
							},
							dataType: 'json',
							header: {
								"Content-Type": "application/x-www-form-urlencoded",
								"Cookie": []
							},
							success: (res) => {
								if (res.data.ret == 0) {
									uni.hideLoading();
									uni.showToast({
										icon: "success",
										title: "验证码发送成功",
										mask: true
									})
									this.timer = new Timer({
										type: '1',
										num: 60
									});

									this.timer.start({
										success: function(res) {
											_this.count_time_text = res + 's';
										},
										complate: function() {
											_this.count_time_text = '获取验证码';
											setTimeout(() => {
												_this.isSentCode = true;
											}, 300)
										}
									})
									uni.setStorage({
										'key': 'cookiekey',
										'data': res.header['Set-Cookie']
									})
								} else {
									uni.showToast({
										icon: "none",
										image: "../../static/images/error.png",
										title: res.data.message,
										mask: true
									})
									setTimeout(() => {
										_this.count_time_text = '获取验证码';
										_this.isSentCode = true;
									}, 300)
								}
							},
							fail: (err) => {

							}
						})					 

					}

				}

				if (this.phone_code_type == 0) {
					let res = await this.$api.user_api.sendsms({
						tel: this.phone,
						type: type
					})
					uni.showLoading({
						mask: true,
						title: '验证码发送中...'
					})
					if (this.isSentCode) {
						this.isSentCode = false;
						if (res.data.ret == 0) {
							uni.hideLoading();
							uni.showToast({
								icon: "success",
								title: "验证码发送成功",
								mask: true
							})
							this.timer = new Timer({
								type: '1',
								num: 60
							});

							this.timer.start({
								success: function(res) {
									_this.count_time_text = res + 's';
								},
								complate: function() {
									_this.count_time_text = '获取验证码';
									setTimeout(() => {
										_this.isSentCode = true;
									}, 300)
								}
							})
						} else {
							uni.hideLoading();
							uni.showToast({
								icon: "none",
								image: "../../static/images/error.png",
								title: res.data.message,
								mask: true
							})
							setTimeout(() => {
								_this.count_time_text = '获取验证码';
								_this.isSentCode = true;
							}, 300)
						}

					}
				}



			},

			// 检测验证码			
			checkSms: async function() {
				let _this = this;
				if (this.phone_code == '') {
					uni.showToast({
						icon: "none",
						title: "请输入验证码",
						mask: true
					})
					return
				} else {
					if (this.isStop) {
						this.isStop = false;
						uni.showLoading({
							mask: true,
							title: '验证码检测中...'
						})
						let res = await this.$api.user_api.checksms({
							code: this.phone_code,
							type: this.phone_code_type
						})
						console.log(res)
						if (res.data.ret == 0) {
							uni.hideLoading();
							this.phone_code = '';
							this.showPopup('hide');
							if (this.phone_code_type == 0) { // 登录
								uni.showToast({
									icon: "success",
									title: "登录成功",
									mask: true,
									duration: 1000,
									async success() {
										let save_cookie = await _this.$api.user_api.saveCookie();
										uni.setStorage({
											key: 'userInfo',
											data: _this.userInfo
										})
										setTimeout(() => {
											uni.switchTab({
												url: "/pages/index/index"
											})
										}, 300)
									}
								})
							} else if (this.phone_code_type == 1) { // 找回密码
								this.showPopup('show', 'isRestPassword');
							}
							setTimeout(() => {
								_this.isStop = true;
							}, 300)

						} else {
							uni.hideLoading();
							uni.showToast({
								icon: "none",
								image: "../../static/images/error.png",
								title: res.data.message,
								mask: true
							})
							setTimeout(() => {
								_this.isStop = true;
							}, 300)
						}
					}
				}

			},

			// 弹窗
			showPopup: function(t, n) {
				if (t == 'show') {
					this.flag.isFullPage = true;
					this.flag[n] = true;
				} else if (t == 'hide') {
					for (let i in this.flag) {
						this.flag[i] = false;
					}
				}
			},


		},
		onUnload() {
			if (this.timer) {
				this.timer.stop();
			}
		},
		computed: {

		},
		components: {

		},
	}
</script>

<style lang="less" scoped>
	.page,
	page {
		background: #e62b23;
	}
	.close-btn{
		position: fixed;
		right:20rpx;
		top:80rpx;
		z-index: 100;
		color:#fff;
	}
	.banner {
		image {
			width: 100%;
		}

		margin-bottom: 30rpx;
	}


	.login-wrap {
		.active {
			display: block;
		}
	}

	.login-form {
		@h: 90rpx;
		width: 90%;
		margin: 0 auto 40rpx;
		display: none;

		.item {
			display: flex;
			width: 90%;
			margin: 0 auto 40rpx;
			height: @h;
			line-height: @h;
			background: rgba(255, 255, 255, .4);
			border-radius: 10rpx;

			.icon {
				width: @h;
				height: @h;
				text-align: center;
				line-height: @h;
				flex-shrink: 0;

				image {
					width: 50%;
					vertical-align: middle;
				}
			}

			input {
				flex-grow: 2;
				height: @h;
				line-height: @h;
				font-size: 30rpx;
				color: #fff;
			}

			.get-code-btn {
				margin-right: 1rem;
				color: #fff;
				font-size: 30rpx;
			}
		}

		.btn {
			width: 90%;
			margin: 0 auto 20rpx;
			text-align: center;
			height: @h;
			line-height: @h;
			background: #f8f8f8;
			border-radius: 10rpx;
			color: #ea544e;
			font-size: 34rpx;
		}
	}
	
	.register-text{
		padding: 0 5% ;
		margin-bottom: 100rpx;
		text-align: right;
		font-size: 30rpx;
		color:#fff;
	}
	
	.re-pw {
		display: flex;
		height: 40rpx;
		line-height: 40rpx;

		radio {
			position: relative;
			transform: scale(0.7);
			color: #FFFFFF;
		}

		.active {
			color: #FFFFFF;
		}

		.re-pw-text {
			font-size: 32rpx;
			color: #ffffff;
		}
	}

	.link-bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 5% 50rpx;

		.find-pw-text {
			height: 40rpx;
			line-height: 40rpx;
			font-size: 30rpx;
			color: #fff;
		}

	}

	.agree-bar {
		display: flex;
		align-items: center;
		padding: 0 5% 50rpx;
		color: #fff;
		font-size: 28rpx;

		radio {
			transform: scale(.7);
			color: #ff9600
		}

		.re-pw-text {
			display: flex;
		}

		navigator {
			color: #ff9600;
		}
	}

	.agreerule {
		display: flex;
		text-align: center;
		justify-content: center;
		position: fixed;
		left: 0;
		width: 100%;
		bottom: 40rpx;

		.icon {
			margin-right: 10rpx;
			color: #E5E5E5;
		}

		.active {
			color: #000;
		}

		.agreerule-text {
			display: flex;
			font-size: 30rpx;
			color: #999;

			navigator {
				color: #f00;
			}
		}
	}

	.code-bar {
		display: flex;
		height: 80rpx;
		border: 1rpx solid #eee;
		border-radius: 15rpx;
		margin-bottom: 40rpx;

		input {
			width: 90%;
			height: 80rpx;
			line-height: 80rpx;
			padding: 0 10rpx;
			font-size: 30rpx;
		}

		.get-code-btn {
			flex-shrink: 0;
			width: 35%;
			line-height: 80rpx;
			text-align: center;
			font-size: 30rpx;
			color: #09f;
		}
	}

	.get-code-popup {
		.phone-text {
			margin-bottom: 30rpx;
			color: #666;
			font-size: 30rpx;
		}

		.code-comfirm-btn {
			height: 70rpx;
			line-height: 70rpx;
			margin: 0 auto;
			background: #ddd;
			border-radius: 8rpx;
			color: #999;
			text-align: center;
			font-size: 30rpx;
		}

		.active {
			background: linear-gradient(#e96b5c, #ce3c2f);
			color: #fff;
		}
	}
</style>
